package org.e_gold.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.e_gold.dao.EventDAO;
import org.e_gold.model.Event;
import org.e_gold.service.EventService;
import org.e_gold.util.Bet365;
import org.e_gold.util.CalcUtil;
import org.e_gold.util.StringUtil;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EventServiceImpl implements EventService, Bet365 {

    private static final Logger logger = Logger.getLogger(EventServiceImpl.class);

    @Autowired
    private EventDAO eventDAO;

    @Override
    // production - backup
    public List<Event> exposeAllEvent(Elements eEvents) {

        List<Event> events = new ArrayList<Event>();
        for (int i = 1; i <= (eEvents.size() - 3); i++) {
            Element e = eEvents.get(i - 1);
            try {
                Event event = new Event();
                String eventName = StringUtil.getName(e.text());
                event.setName(eventName);
                event.setHomeName(eventName.split(" v ")[0]);
                event.setAwayName(eventName.split(" v ")[1]);
                event.setTotalGoal(CalcUtil.getTotalGoal(e.select(".Score").get(0).text()));
                event.setLocation(e.select("a").attr("href"));

                event.setLocationFull(BASE_SPORT + e.select("a").attr("href").substring(3));
                event.setPlayTime(CalcUtil.getCurrentTime(e.select("#ct" + i).get(0).text()));
                logger.info(":::::::::::::::");
                logger.info("Event name : " + StringUtil.getName(e.text()));
                logger.info(e.select(".Score").get(0).text());
                logger.info(e.select("#ct" + i).get(0).text());
                events.add(event);
            } catch (Exception ex) {
                logger.error("Parse error : " + e.text());
                logger.error(e);
            }
        }
        return events;
    }

    // @Override
    // // development
    // public List<Event> parseEvent(Elements eEvents) {
    //
    // List<Event> events = new ArrayList<Event>();
    // for (int i = 1; i <= (eEvents.size() - 3); i++) {
    // Element e = eEvents.get(i - 1);
    // try {
    // Event event = new Event();
    // String eventName = StringUtil.getName(e.text());
    // event.setName(eventName);
    // event.setHomeName(eventName.split(" v ")[0]);
    // event.setAwayName(eventName.split(" v ")[1]);
    // event.setTotalGoal(CalcUtil.getTotalGoal(e.select(".Score").get(0).text()));
    // event.setLocation(e.select("a").attr("href"));
    // event.setPlayTime(CalcUtil.getCurrentTime(e.select("#ct" + i).get(0).text()));
    // if (event.getPlayTime() > 80) {
    // events.add(event);
    // }
    // } catch (Exception ex) {
    // }
    //
    // }
    // return events;
    // }

    @Override
    public List<Event> persistEvent(List<Event> events) {

        List<Event> savedEvent = new ArrayList<Event>();
        for (Event event : events) {
            // insert new or populate old event: this is good solution :D ^____^
            Event eventPersisted = eventDAO.findByName(event.getName());
            if (eventPersisted == null) {
                eventPersisted = eventDAO.insert(event);
            }
            eventPersisted.setTotalGoal(event.getTotalGoal());
            eventPersisted.setPlayTime(event.getPlayTime());
            eventDAO.update(eventPersisted);
            savedEvent.add(eventPersisted);
        }
        return savedEvent;
    }
}
